package com.itstyle.jpa.web;

import com.itstyle.jpa.entity.Result;
import com.itstyle.jpa.model.User;
import com.itstyle.jpa.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @name: UserController
 * @Author: wangshimin
 * @Date: 2019-08-05  11:21
 * @Description:
 */
@Api("用户端口")
@Controller
public class UserController {
    @Autowired
    private IUserService iUserService;

    @PostMapping("/listUsers")
    @ResponseBody
    public List<User> findAll(){
        return iUserService.findAll();
    }

    @PostMapping("/findUserByUserName")
    @ResponseBody
    public List<User>  findUserByUserName(String usName){
        return iUserService.findUserByUserName(usName);
    }



    @PostMapping("/deleteUser")
    @ResponseBody
    public Result deleteUser(Integer min,Integer max){
        iUserService.deleteUserByAgeBetween(min,max);
        return Result.ok();
    }
    @PostMapping("/findUserByPage")
    @ResponseBody
    public Page<User> findUserByPage(Integer pageNum, Integer pageSize){
        return iUserService.findByPage(pageNum,pageSize);
    }


    /**
     * 添加学生
     * @return
     */
    @PostMapping("/saveUser")
    @ResponseBody
    public User save(Integer age,String name){
        User user=new User(name,age);
        return iUserService.saveAndFulsh(user);
    }

    @ResponseBody
    @PostMapping("/count")
    public long count(){
        return iUserService.count();
    }
}
